﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Data;
using NLBS.Entity;
using NLBS.Data.Business;
using NLBS.Views;

namespace NLBS.Web.UI
{
    public class CategoryCommand : ICommand, ICategoryAction
    {

        #region Presenter

        private CategoryPresenter _presenter;

        public CategoryPresenter Presenter
        {
            get
            {
                if (this._presenter == null)
                    _presenter = new CategoryPresenter();
                this._presenter.Action = this;
                return _presenter;
            }
        }

        private void PresenterInit()
        {
            this.Presenter.OnActionLoaded();
        }
        #endregion


        private Action act;
        private string[] parameters;
        public CategoryCommand(Action act, string[] parameters)
        {
            this.act = act;
            this.parameters = parameters;
        }

        #region ICommand Members

        public void Excute()
        {
            PresenterInit();
            switch (act)
            {
                case Action.Insert:
                    saveCategory();
                    break;
                case Action.Delete:
                    deleteCategory();
                    break;
                case Action.Update:
                    moveCategory();
                    break;
            }
        }



        private void saveCategory()
        {
            int b = parameters[1].IndexOf(",");
            if (b > 0)
            {
                string[] catList = parameters[1].Split(new char[] { ',' });
                string[] titleList = parameters[2].Split(new char[] { ',' });
                string[] hiddenList = parameters[3].Split(new char[] { ',' });
                string[] lockList = parameters[4].Split(new char[] { ',' });
                string[] orderList = parameters[5].Split(new char[] { ',' });
                for (int h = 0; h < catList.Length; h++)
                {
                    this.keyID = Convert.ToInt32(catList[h]);
                    this.DataSelectSingle(null, null);
                    NLBS.Entity.Category updatecat = this.Entity;
                    updatecat.CategoryID = Convert.ToInt32(catList[h]);
                    updatecat.Name = titleList[h];
                    updatecat.Hidden = bool.Parse(hiddenList[h]);
                    updatecat.Locked = bool.Parse(lockList[h]);
                    updatecat.Order = int.Parse(orderList[h]);
                    this.DataUpdate(null, null);
                }
            }
            if (parameters.Length > 6)
            {
                addCategory();
            }
        }

        private void addCategory()
        {
            Category cat = new Category();
            string[] newCat = parameters[6].Split(new char[] { ',' });
            cat.Name = newCat[0];
            cat.Hidden = bool.Parse(newCat[1]);
            cat.Locked = bool.Parse(newCat[2]);
            cat.Order = Convert.ToInt32(newCat[3]);
            this.Entity = cat;
            this.DataInsert(null, null);
        }

        private void deleteCategory()
        {
            for (int i = 0; i < parameters.Length; i++)
            {
                if (i > 0)
                {
                    this.keyID = Convert.ToInt32(parameters[i]);
                    this.DataDelete(null, null);
                }
            }
        }

        private void moveCategory()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i < parameters.Length - 1; i++)
            {
                sb.Append(parameters[i]);
                if (i < (parameters.Length - 2))
                    sb.Append(",");

            }
            this.keyID = Convert.ToInt32(parameters[parameters.Length - 1]);
            this.MoveCatList = sb.ToString();
            this.DataMove(null, null);
        }

        #endregion

        #region IAction<Category> Members



        public event EventHandler DataInsert;

        public event EventHandler DataUpdate;

        public event EventHandler DataDelete;

        public event EventHandler DataMove;

        public event EventHandler DataSelectSingle;

        private Category _entity;
        public Category Entity
        {
            get
            {
                return _entity;
            }
            set
            {
                _entity = value;
            }
        }

        private int _keyID;
        public int keyID
        {
            get
            {
                return _keyID;
            }
            set
            {
                _keyID = value;
            }
        }



        private string _moveCatList;
        public string MoveCatList
        {
            get
            {
                return _moveCatList;
            }
            set
            {
                _moveCatList = value;
            }
        }



        #endregion
    }
}
